<template>
  <!-- 添加或修改企业备案对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
      <el-form-item label="信用代码" prop="entCode">
        <el-input v-model.trim="form.entCode" placeholder="请输入信用代码" />
      </el-form-item>

      <el-form-item label="企业名称" prop="entName">
        <el-input v-model.trim="form.entName" type="textarea" placeholder="请输入内容" />
      </el-form-item>

      <el-form-item label="企业简称" prop="entShortName">
        <el-input v-model.trim="form.entShortName" placeholder="请输入企业简称" />
      </el-form-item>

      <el-form-item label="法人" prop="legalPerson">
        <el-input v-model.trim="form.legalPerson" placeholder="请输入法人" />
      </el-form-item>

      <el-form-item label="联系人" prop="linkman">
        <el-input v-model.trim="form.linkman" placeholder="请输入联系人" />
      </el-form-item>

      <el-form-item label="联系电话" prop="telephone">
        <el-input v-model.trim="form.telephone" placeholder="请输入联系电话" />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" placeholder="请输入邮箱" />
      </el-form-item>

      <el-form-item label="企业地址" prop="address">
        <el-input v-model.trim="form.address" placeholder="请输入企业地址" />
      </el-form-item>

      <el-form-item label="报关资质" prop="cdqMark">
        <el-select v-model.trim="form.cdqMark" placeholder="请选择报关资质">
          <el-option v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="企业状态" prop="status">
        <el-select v-model.trim="form.status" placeholder="请选择企业状态">
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="企业类型" prop="entType">
        <el-select v-model.trim="form.entType" @change="entTypeChange" placeholder="请选择企业类型">
          <el-option v-for="dict in dict.type.ent_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="场所代码" prop="fieldCode" v-show="form.entType=='3'">
        <el-input v-model.trim="form.fieldCode" placeholder="请输入场所代码" />
      </el-form-item>

      <el-form-item label="访问平台" prop="platformCodes">
        <el-select v-model.trim="form.platformCodes" multiple filterable clearable placeholder="请选择访问平台">
          <el-option
            v-for="item in systemList"
            :key="item.platformId"
            :label="item.platformName"
            :value="item.platformSource"
          ></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="营业执照" prop="licenseImg">
        <el-input v-model.trim="form.licenseImg" placeholder="请输入营业执照" />
      </el-form-item>


      <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="form.remark" placeholder="请输入备注" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getEnterpriseInfo,
    addEnterpriseInfo,
    updateEnterpriseInfo
  } from "@/api/system/enterpriseInfo";

  import {listPlatform} from "@/api/system/platform";

  export default {
    name: "EnterpriseInfo-addEdit",
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 子系统列表
        systemList: [],
        // 表单参数
        form: {
          id: null,
          entCode: null,
          entName: null,
          entShortName: null,
          legalPerson: null,
          linkman: null,
          telephone: null,
          email: null,
          address: null,
          cdqMark: null,
          status: null,
          entType: null,
          licenseImg: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          platformCodes:null,
          fieldCode:null

        },
        // 企业备案表格数据
        enterpriseInfoList: [],
        // 表单校验
        rules: {
          entCode: [{
            required: true,
            message: "信用代码不能为空",
            trigger: "blur"
          }],
          fieldCode: [{
            required: false,
            message: "场所代码不能为空",
            trigger: "blur"
          }],
          entName: [{
            required: true,
            message: "企业名称不能为空",
            trigger: "blur"
          }],
          entShortName: [{
            required: true,
            message: "企业简称不能为空",
            trigger: "blur"
          }],
          legalPerson: [{
            required: true,
            message: "法人不能为空",
            trigger: "blur"
          }],
          linkman: [{
            required: true,
            message: "联系人不能为空",
            trigger: "blur"
          }],
          telephone: [{
            required: true,
            message: "联系电话不能为空",
            trigger: "blur"
          }],
          address: [{
            required: true,
            message: "企业地址不能为空",
            trigger: "blur"
          }],
          cdqMark: [{
            required: true,
            message: "报关资质不能为空",
            trigger: "change"
          }],
          status: [{
            required: true,
            message: "企业状态不能为空",
            trigger: "change"
          }],
          entType: [{
            required: true,
            message: "企业类型不能为空",
            trigger: "change"
          }],
          createBy: [{
            required: true,
            message: "创建人不能为空",
            trigger: "blur"
          }],
          createTime: [{
            required: true,
            message: "创建时间不能为空",
            trigger: "blur"
          }],
          platformCodes :[{
            required: true,
            message: "访问平台不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
           this.getPlatformList();
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const id = this.rowData.id;
        getEnterpriseInfo(id).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
            this.form.platformCodes=this.form.platformCodes.split(",");
          }
        });
      },
      // 获取系统列表
      getPlatformList() {
        listPlatform().then((response) => {
          this.systemList = response.rows;
        });
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            params.platformCodes=params.platformCodes.join(",");

            if (this.dialogType == "add") {
              addEnterpriseInfo(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateEnterpriseInfo(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
      entTypeChange(val){
        if(val=='3'){
          this.rules.fieldCode[0].required=true;
        }else{
           this.rules.fieldCode[0].required=false;
        }
      }
    },
  };
</script>

<style lang="scss" scoped></style>
